package edu.unf.cen6940.web;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.unf.cen6940.dao.CategoryDAO;
import edu.unf.cen6940.dao.FacultyDAO;
import edu.unf.cen6940.dao.GeneratedproductDAO;
import edu.unf.cen6940.dao.StudentDAO;
import edu.unf.cen6940.dao.ThesisDAO;
import edu.unf.cen6940.dao.ThesiscommitteeDAO;
import edu.unf.cen6940.dao.ThesisstatusDAO;
import edu.unf.cen6940.dao.ThesistoolDAO;
import edu.unf.cen6940.domain.Category;
import edu.unf.cen6940.domain.Faculty;
import edu.unf.cen6940.domain.Generatedproduct;
import edu.unf.cen6940.domain.Student;
import edu.unf.cen6940.domain.Thesis;
import edu.unf.cen6940.domain.Thesiscommittee;
import edu.unf.cen6940.domain.Thesisstatus;
import edu.unf.cen6940.domain.Thesistool;
import edu.unf.cen6940.service.ThesisService;

/**
 * Spring MVC controller that handles CRUD requests for Thesis entities
 * 
 */

@Controller("ThesisController") 	
public class ThesisController {

	/**
	 * DAO injected by Spring that manages Category entities
	 * 
	 */
	@Autowired
	private CategoryDAO categoryDAO;

	/**
	 * DAO injected by Spring that manages Generatedproduct entities
	 * 
	 */
	@Autowired
	private GeneratedproductDAO generatedproductDAO;

	/**
	 * DAO injected by Spring that manages Student entities
	 * 
	 */
	@Autowired
	private StudentDAO studentDAO;

	/**
	 * DAO injected by Spring that manages Thesis entities
	 * 
	 */
	@Autowired
	private ThesisDAO thesisDAO;

	/**
	 * DAO injected by Spring that manages Thesiscommittee entities
	 * 
	 */
	@Autowired
	private ThesiscommitteeDAO thesiscommitteeDAO;

	/**
	 * DAO injected by Spring that manages Thesisstatus entities
	 * 
	 */
	@Autowired
	private ThesisstatusDAO thesisstatusDAO;

	/**
	 * DAO injected by Spring that manages Thesistool entities
	 * 
	 */
	@Autowired
	private ThesistoolDAO thesistoolDAO;

	
	@Autowired
	private FacultyDAO facultyDAO;
	
	/**
	 * Service injected by Spring that provides CRUD operations for Thesis entities
	 * 
	 */
	@Autowired
	private ThesisService thesisService;

	/**
	 * Delete an existing Thesistool entity
	 * 
	 */
	@RequestMapping("/deleteThesisThesistools")
	public ModelAndView deleteThesisThesistools(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_thesistools_thesisToolId) {
		ModelAndView mav = new ModelAndView();

		Thesis thesis = thesisService.deleteThesisThesistools(thesis_thesisId, related_thesistools_thesisToolId);

		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		//binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm aaa");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	/**
	 * Delete an existing Thesiscommittee entity
	 * 
	 */
	@RequestMapping("/deleteThesisThesiscommittees")
	public ModelAndView deleteThesisThesiscommittees(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_thesiscommittees_thesisCommitteeId) {
		ModelAndView mav = new ModelAndView();

		Thesis thesis = thesisService.deleteThesisThesiscommittees(thesis_thesisId, related_thesiscommittees_thesisCommitteeId);

		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Save an existing Category entity
	 * 
	 */
	@RequestMapping("/saveThesisCategories")
	public ModelAndView saveThesisCategories(@RequestParam Integer thesis_thesisId, @ModelAttribute Category categories) {
		Thesis parent_thesis = thesisService.saveThesisCategories(thesis_thesisId, categories);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", parent_thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Delete an existing Student entity
	 * 
	 */
	@RequestMapping("/deleteThesisStudent")
	public ModelAndView deleteThesisStudent(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_student_studentId) {
		ModelAndView mav = new ModelAndView();

		Thesis thesis = thesisService.deleteThesisStudent(thesis_thesisId, related_student_studentId);

		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Entry point to show all Thesis entities
	 * 
	 */
	public String indexThesis() {
		return "redirect:/indexThesis";
	}

	/**
	 * Select the child Category entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteThesisCategories")
	public ModelAndView confirmDeleteThesisCategories(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_categories_categoryId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("category", categoryDAO.findCategoryByPrimaryKey(related_categories_categoryId));
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.setViewName("thesis/categories/deleteCategories");

		return mav;
	}

	/**
	 * Edit an existing Thesistool entity
	 * 
	 */
	@RequestMapping("/editThesisThesistools")
	public ModelAndView editThesisThesistools(@RequestParam Integer thesis_thesisId, @RequestParam Integer thesistools_thesisToolId) {
		Thesistool thesistool = thesistoolDAO.findThesistoolByPrimaryKey(thesistools_thesisToolId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesistool", thesistool);
		mav.setViewName("thesis/thesistools/editThesistools");

		return mav;
	}

	/**
	 * Delete an existing Generatedproduct entity
	 * 
	 */
	@RequestMapping("/deleteThesisGeneratedproducts")
	public ModelAndView deleteThesisGeneratedproducts(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_generatedproducts_generatedProductId) {
		ModelAndView mav = new ModelAndView();

		Thesis thesis = thesisService.deleteThesisGeneratedproducts(thesis_thesisId, related_generatedproducts_generatedProductId);

		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Select the child Generatedproduct entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteThesisGeneratedproducts")
	public ModelAndView confirmDeleteThesisGeneratedproducts(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_generatedproducts_generatedProductId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("generatedproduct", generatedproductDAO.findGeneratedproductByPrimaryKey(related_generatedproducts_generatedProductId));
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.setViewName("thesis/generatedproducts/deleteGeneratedproducts");

		return mav;
	}

	/**
	 * Edit an existing Thesisstatus entity
	 * 
	 */
	@RequestMapping("/editThesisThesisstatuses")
	public ModelAndView editThesisThesisstatuses(@RequestParam Integer thesis_thesisId, @RequestParam Integer thesisstatuses_thesisStatusId) {
		Thesisstatus thesisstatus = thesisstatusDAO.findThesisstatusByPrimaryKey(thesisstatuses_thesisStatusId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesisstatus", thesisstatus);
		mav.setViewName("thesis/thesisstatuses/editThesisstatuses");

		return mav;
	}

	/**
	 * Select the child Student entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteThesisStudent")
	public ModelAndView confirmDeleteThesisStudent(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_student_studentId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("student", studentDAO.findStudentByPrimaryKey(related_student_studentId));
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.setViewName("thesis/student/deleteStudent");

		return mav;
	}

	/**
	 * Save an existing Student entity
	 * 
	 */
	@RequestMapping("/saveThesisStudent")
	public ModelAndView saveThesisStudent(@RequestParam Integer thesis_thesisId, @ModelAttribute Student student) {
		Thesis parent_thesis = thesisService.saveThesisStudent(thesis_thesisId, student);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", parent_thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Select the child Thesistool entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteThesisThesistools")
	public ModelAndView confirmDeleteThesisThesistools(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_thesistools_thesisToolId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesistool", thesistoolDAO.findThesistoolByPrimaryKey(related_thesistools_thesisToolId));
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.setViewName("thesis/thesistools/deleteThesistools");

		return mav;
	}

	/**
	 * Show all Student entities by Thesis
	 * 
	 */
	@RequestMapping("/listThesisStudent")
	public ModelAndView listThesisStudent(@RequestParam Integer thesisIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(thesisIdKey));
		mav.setViewName("thesis/student/listStudent");

		return mav;
	}

	/**
	 * Show all Thesiscommittee entities by Thesis
	 * 
	 */
	@RequestMapping("/listThesisThesiscommittees")
	public ModelAndView listThesisThesiscommittees(@RequestParam Integer thesisIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(thesisIdKey));
		mav.setViewName("thesis/thesiscommittees/listThesiscommittees");

		return mav;
	}

	/**
	 * Create a new Generatedproduct entity
	 * 
	 */
	@RequestMapping("/newThesisGeneratedproducts")
	public ModelAndView newThesisGeneratedproducts(@RequestParam Integer thesis_thesisId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("generatedproduct", new Generatedproduct());
		mav.addObject("newFlag", true);
		Set<Generatedproduct> generatedproductSet = generatedproductDAO.findAllGeneratedproducts();
		Map<Integer,String> generatedproductData = new HashMap<Integer, String>();
		for(Generatedproduct generatedproduct:generatedproductSet){
			generatedproductData.put(generatedproduct.getGeneratedProductId(),generatedproduct.getProductName());
		}
		mav.addObject("generatedproductList",generatedproductData);
		mav.setViewName("thesis/generatedproducts/editGeneratedproducts");

		return mav;
	}

	/**
	 * Select an existing Thesis entity
	 * 
	 */
	@RequestMapping("/selectThesis")
	public ModelAndView selectThesis(@RequestParam Integer thesisIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(thesisIdKey));
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Show all Thesistool entities by Thesis
	 * 
	 */
	@RequestMapping("/listThesisThesistools")
	public ModelAndView listThesisThesistools(@RequestParam Integer thesisIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(thesisIdKey));
		mav.setViewName("thesis/thesistools/listThesistools");

		return mav;
	}

	/**
	 * Select the child Thesisstatus entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteThesisThesisstatuses")
	public ModelAndView confirmDeleteThesisThesisstatuses(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_thesisstatuses_thesisStatusId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesisstatus", thesisstatusDAO.findThesisstatusByPrimaryKey(related_thesisstatuses_thesisStatusId));
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.setViewName("thesis/thesisstatuses/deleteThesisstatuses");

		return mav;
	}

	/**
	 * Edit an existing Student entity
	 * 
	 */
	@RequestMapping("/editThesisStudent")
	public ModelAndView editThesisStudent(@RequestParam Integer thesis_thesisId, @RequestParam Integer student_studentId) {
		Student student = studentDAO.findStudentByPrimaryKey(student_studentId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("student", student);
		mav.setViewName("thesis/student/editStudent");

		return mav;
	}

	/**
	 * Create a new Thesiscommittee entity
	 * 
	 */
	@RequestMapping("/newThesisThesiscommittees")
	public ModelAndView newThesisThesiscommittees(@RequestParam Integer thesis_thesisId) {
		System.out.println("ThesisController.newThesisThesiscommittees()=====");
		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesiscommittee", new Thesiscommittee());
		mav.addObject("newFlag", true);
		Set<Faculty> facultySet = facultyDAO.findAllFacultys();
		Map<Integer,String> facultyData = new HashMap<Integer, String>();
		for(Faculty faculty:facultySet){
			facultyData.put(faculty.getFacultyId(),faculty.getFirstName()+" "+faculty.getLastName());
		}
		mav.addObject("facultyList",facultyData);
		/*Set<Thesiscommittee> thesiscommitteeSet = thesiscommitteeDAO.findAllThesiscommittees();
		Map<Integer,String> thesiscommitteeData = new HashMap<Integer, String>();
		for(Thesiscommittee thesiscommittee:thesiscommitteeSet){
			thesiscommitteeData.put(thesiscommittee.getThesisCommitteeId(),
					thesiscommittee.getFacultyByChair().getLastName()+"| "+thesiscommittee.getFacultyByChair().getFirstName()
					+((thesiscommittee.getFacultyByCommitteeMember1() != null)?" "+thesiscommittee.getFacultyByCommitteeMember1().getLastName()+"| "+thesiscommittee.getFacultyByCommitteeMember1().getFirstName():"")
					+((thesiscommittee.getFacultyByCommitteeMember2() != null)?" "+thesiscommittee.getFacultyByCommitteeMember2().getLastName()+"| "+thesiscommittee.getFacultyByCommitteeMember2().getFirstName():"")
					+((thesiscommittee.getFacultyByCommitteeMember3() != null)?" "+thesiscommittee.getFacultyByCommitteeMember3().getLastName()+"| "+thesiscommittee.getFacultyByCommitteeMember3().getFirstName():"")
					+((thesiscommittee.getFacultyByCommitteeMember4() != null)?" "+thesiscommittee.getFacultyByCommitteeMember4().getLastName()+"| "+thesiscommittee.getFacultyByCommitteeMember4().getFirstName():"")
					+((thesiscommittee.getFacultyByCommitteeMember5() != null)?" "+thesiscommittee.getFacultyByCommitteeMember5().getLastName()+"| "+thesiscommittee.getFacultyByCommitteeMember5().getFirstName():"")
					);
		}
		mav.addObject("thesiscommitteeList",thesiscommitteeData);*/
		mav.setViewName("thesis/thesiscommittees/editThesiscommittees");

		return mav;
	}

	/**
	 * View an existing Student entity
	 * 
	 */
	@RequestMapping("/selectThesisStudent")
	public ModelAndView selectThesisStudent(@RequestParam Integer thesis_thesisId, @RequestParam Integer student_studentId) {
		Student student = studentDAO.findStudentByPrimaryKey(student_studentId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("student", student);
		mav.setViewName("thesis/student/viewStudent");

		return mav;
	}

	/**
	 * Save an existing Thesis entity
	 * 
	 */
	@RequestMapping("/saveThesis")
	public String saveThesis(@ModelAttribute Thesis thesis) {
		thesisService.saveThesis(thesis);
		return "forward:/indexThesis";
	}

	/**
	 */
	@RequestMapping("/thesisController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Create a new Thesistool entity
	 * 
	 */
	@RequestMapping("/newThesisThesistools")
	public ModelAndView newThesisThesistools(@RequestParam Integer thesis_thesisId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesistool", new Thesistool());
		mav.addObject("newFlag", true);
		Set<Thesistool> thesistoolSet = thesistoolDAO.findAllThesistools();
		Map<Integer,String> thesistoolData = new HashMap<Integer, String>();
		for(Thesistool thesistool:thesistoolSet){
			thesistoolData.put(thesistool.getThesisToolId(),thesistool.getToolDescription());
		}
		mav.addObject("thesistoolList",thesistoolData);
		mav.setViewName("thesis/thesistools/editThesistools");

		return mav;
	}

	/**
	 * Save an existing Thesisstatus entity
	 * 
	 */
	@RequestMapping("/saveThesisThesisstatuses")
	public ModelAndView saveThesisThesisstatuses(@RequestParam Integer thesis_thesisId, @ModelAttribute Thesisstatus thesisstatuses) {
		Thesis parent_thesis = thesisService.saveThesisThesisstatuses(thesis_thesisId, thesisstatuses);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", parent_thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Edit an existing Generatedproduct entity
	 * 
	 */
	@RequestMapping("/editThesisGeneratedproducts")
	public ModelAndView editThesisGeneratedproducts(@RequestParam Integer thesis_thesisId, @RequestParam Integer generatedproducts_generatedProductId) {
		Generatedproduct generatedproduct = generatedproductDAO.findGeneratedproductByPrimaryKey(generatedproducts_generatedProductId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("generatedproduct", generatedproduct);
		mav.setViewName("thesis/generatedproducts/editGeneratedproducts");

		return mav;
	}

	/**
	 * View an existing Thesistool entity
	 * 
	 */
	@RequestMapping("/selectThesisThesistools")
	public ModelAndView selectThesisThesistools(@RequestParam Integer thesis_thesisId, @RequestParam Integer thesistools_thesisToolId) {
		Thesistool thesistool = thesistoolDAO.findThesistoolByPrimaryKey(thesistools_thesisToolId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesistool", thesistool);
		mav.setViewName("thesis/thesistools/viewThesistools");

		return mav;
	}

	/**
	 * Create a new Thesisstatus entity
	 * 
	 */
	@RequestMapping("/newThesisThesisstatuses")
	public ModelAndView newThesisThesisstatuses(@RequestParam Integer thesis_thesisId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesisstatus", new Thesisstatus());
		mav.addObject("newFlag", true);
		mav.setViewName("thesis/thesisstatuses/editThesisstatuses");

		return mav;
	}

	/**
	 * Edit an existing Category entity
	 * 
	 */
	@RequestMapping("/editThesisCategories")
	public ModelAndView editThesisCategories(@RequestParam Integer thesis_thesisId, @RequestParam Integer categories_categoryId) {
		Category category = categoryDAO.findCategoryByPrimaryKey(categories_categoryId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("category", category);
		mav.setViewName("thesis/categories/editCategories");

		return mav;
	}

	/**
	 * Save an existing Thesiscommittee entity
	 * 
	 */
	@RequestMapping("/saveThesisThesiscommittees")
	public ModelAndView saveThesisThesiscommittees(@RequestParam Integer thesis_thesisId, @ModelAttribute Thesiscommittee thesiscommittees) {
		Thesis parent_thesis = thesisService.saveThesisThesiscommittees(thesis_thesisId, thesiscommittees);
		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", parent_thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Show all Thesisstatus entities by Thesis
	 * 
	 */
	@RequestMapping("/listThesisThesisstatuses")
	public ModelAndView listThesisThesisstatuses(@RequestParam Integer thesisIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(thesisIdKey));
		mav.setViewName("thesis/thesisstatuses/listThesisstatuses");

		return mav;
	}

	/**
	 * Create a new Thesis entity
	 * 
	 */
	@RequestMapping("/newThesis")
	public ModelAndView newThesis() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", new Thesis());
		mav.addObject("newFlag", true);
		Set<Student> studentData = studentDAO.findAllStudents();
		Map<Integer,String> studentMap = new HashMap<Integer, String>();
		for(Student student:studentData){
			studentMap.put(student.getStudentId(),student.getFirstName()+" "+student.getLastName());
		}
		mav.addObject("studentList",studentMap);
		mav.setViewName("thesis/editThesis");
		return mav;
	}

	/**
	 * Save an existing Generatedproduct entity
	 * 
	 */
	@RequestMapping("/saveThesisGeneratedproducts")
	public ModelAndView saveThesisGeneratedproducts(@RequestParam Integer thesis_thesisId, @ModelAttribute Generatedproduct generatedproducts) {
		Thesis parent_thesis = thesisService.saveThesisGeneratedproducts(thesis_thesisId, generatedproducts);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", parent_thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Delete an existing Category entity
	 * 
	 */
	@RequestMapping("/deleteThesisCategories")
	public ModelAndView deleteThesisCategories(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_categories_categoryId) {
		ModelAndView mav = new ModelAndView();

		Thesis thesis = thesisService.deleteThesisCategories(thesis_thesisId, related_categories_categoryId);

		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * View an existing Thesiscommittee entity
	 * 
	 */
	@RequestMapping("/selectThesisThesiscommittees")
	public ModelAndView selectThesisThesiscommittees(@RequestParam Integer thesis_thesisId, @RequestParam Integer thesiscommittees_thesisCommitteeId) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesiscommittees_thesisCommitteeId, -1, -1);

		ModelAndView mav = new ModelAndView();
		//mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesiscommittee", thesiscommittee);
		//mav.setViewName("thesis/thesiscommittees/viewThesiscommittees");
		mav.setViewName("thesiscommittee/viewThesiscommittee");

		return mav;
	}

	/**
	 * View an existing Generatedproduct entity
	 * 
	 */
	@RequestMapping("/selectThesisGeneratedproducts")
	public ModelAndView selectThesisGeneratedproducts(@RequestParam Integer thesis_thesisId, @RequestParam Integer generatedproducts_generatedProductId) {
		Generatedproduct generatedproduct = generatedproductDAO.findGeneratedproductByPrimaryKey(generatedproducts_generatedProductId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("generatedproduct", generatedproduct);
		mav.setViewName("thesis/generatedproducts/viewGeneratedproducts");

		return mav;
	}

	/**
	 * Delete an existing Thesis entity
	 * 
	 */
	@RequestMapping("/deleteThesis")
	public String deleteThesis(@RequestParam Integer thesisIdKey) {
		Thesis thesis = thesisDAO.findThesisByPrimaryKey(thesisIdKey);
		thesisService.deleteThesis(thesis);
		return "forward:/indexThesis";
	}

	/**
	 * Show all Thesis entities
	 * 
	 */
	@RequestMapping("/indexThesis")
	public ModelAndView listThesiss() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesiss", thesisService.loadThesiss());

		mav.setViewName("thesis/listThesiss");

		return mav;
	}

	/**
	 * Create a new Category entity
	 * 
	 */
	@RequestMapping("/newThesisCategories")
	public ModelAndView newThesisCategories(@RequestParam Integer thesis_thesisId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("category", new Category());
		Set<Category> categorySet = categoryDAO.findAllCategorys();
		Map<Integer,String> categoryData = new HashMap<Integer, String>();
		for(Category category:categorySet){
			categoryData.put(category.getCategoryId(),category.getCategoryDescription());
		}
		mav.addObject("categoryList",categoryData);
		mav.addObject("newFlag", true);
		mav.setViewName("thesis/categories/editCategories");

		return mav;
	}

	/**
	 * Delete an existing Thesisstatus entity
	 * 
	 */
	@RequestMapping("/deleteThesisThesisstatuses")
	public ModelAndView deleteThesisThesisstatuses(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_thesisstatuses_thesisStatusId) {
		ModelAndView mav = new ModelAndView();

		Thesis thesis = thesisService.deleteThesisThesisstatuses(thesis_thesisId, related_thesisstatuses_thesisStatusId);

		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Show all Generatedproduct entities by Thesis
	 * 
	 */
	@RequestMapping("/listThesisGeneratedproducts")
	public ModelAndView listThesisGeneratedproducts(@RequestParam Integer thesisIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(thesisIdKey));
		mav.setViewName("thesis/generatedproducts/listGeneratedproducts");

		return mav;
	}

	/**
	 * Save an existing Thesistool entity
	 * 
	 */
	@RequestMapping("/saveThesisThesistools")
	public ModelAndView saveThesisThesistools(@RequestParam Integer thesis_thesisId, @ModelAttribute Thesistool thesistools) {
		Thesis parent_thesis = thesisService.saveThesisThesistools(thesis_thesisId, thesistools);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesis", parent_thesis);
		mav.setViewName("thesis/viewThesis");

		return mav;
	}

	/**
	 * Create a new Student entity
	 * 
	 */
	@RequestMapping("/newThesisStudent")
	public ModelAndView newThesisStudent(@RequestParam Integer thesis_thesisId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("student", new Student());
		mav.addObject("newFlag", true);
		mav.setViewName("thesis/student/editStudent");

		return mav;
	}

	/**
	 * Edit an existing Thesiscommittee entity
	 * 
	 */
	@RequestMapping("/editThesisThesiscommittees")
	public ModelAndView editThesisThesiscommittees(@RequestParam Integer thesis_thesisId, @RequestParam Integer thesiscommittees_thesisCommitteeId) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesiscommittees_thesisCommitteeId, -1, -1);

		ModelAndView mav = new ModelAndView();
		//mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesiscommittee", thesiscommittee);
		mav.setViewName("/thesiscommittee/editThesiscommittee");

		return mav;
	}

	/**
	 * Select the Thesis entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteThesis")
	public ModelAndView confirmDeleteThesis(@RequestParam Integer thesisIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(thesisIdKey));
		mav.setViewName("thesis/deleteThesis");

		return mav;
	}

	/**
	 * View an existing Thesisstatus entity
	 * 
	 */
	@RequestMapping("/selectThesisThesisstatuses")
	public ModelAndView selectThesisThesisstatuses(@RequestParam Integer thesis_thesisId, @RequestParam Integer thesisstatuses_thesisStatusId) {
		Thesisstatus thesisstatus = thesisstatusDAO.findThesisstatusByPrimaryKey(thesisstatuses_thesisStatusId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("thesisstatus", thesisstatus);
		mav.setViewName("thesis/thesisstatuses/viewThesisstatuses");

		return mav;
	}

	/**
	 * Select the child Thesiscommittee entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteThesisThesiscommittees")
	public ModelAndView confirmDeleteThesisThesiscommittees(@RequestParam Integer thesis_thesisId, @RequestParam Integer related_thesiscommittees_thesisCommitteeId) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesiscommittee", thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(related_thesiscommittees_thesisCommitteeId));
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.setViewName("thesis/thesiscommittees/deleteThesiscommittees");

		return mav;
	}

	/**
	 * Edit an existing Thesis entity
	 * 
	 */
	@RequestMapping("/editThesis")
	public ModelAndView editThesis(@RequestParam Integer thesisIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(thesisIdKey));
		Map<Integer,String> studentMap = new HashMap<Integer, String>();
		Set<Student> studentData = studentDAO.findAllStudents();
		for(Student student:studentData){
			studentMap.put(student.getStudentId(),student.getFirstName()+" "+student.getLastName());
		}
		mav.addObject("studentList",studentMap);
		mav.addObject("studentName",thesisDAO.findThesisByPrimaryKey(thesisIdKey).getStudent().getFirstName()+" "+thesisDAO.findThesisByPrimaryKey(thesisIdKey).getStudent().getLastName());
		mav.addObject("studentId", thesisDAO.findThesisByPrimaryKey(thesisIdKey).getStudent().getStudentId());
		mav.setViewName("thesis/editThesis");

		return mav;
	}

	/**
	 * View an existing Category entity
	 * 
	 */
	@RequestMapping("/selectThesisCategories")
	public ModelAndView selectThesisCategories(@RequestParam Integer thesis_thesisId, @RequestParam Integer categories_categoryId) {
		Category category = categoryDAO.findCategoryByPrimaryKey(categories_categoryId, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("thesis_thesisId", thesis_thesisId);
		mav.addObject("category", category);
		mav.setViewName("thesis/categories/viewCategories");

		return mav;
	}

	/**
	 * Show all Category entities by Thesis
	 * 
	 */
	@RequestMapping("/listThesisCategories")
	public ModelAndView listThesisCategories(@RequestParam Integer thesisIdKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("thesis", thesisDAO.findThesisByPrimaryKey(thesisIdKey));
		mav.setViewName("thesis/categories/listCategories");

		return mav;
	}
	
	@RequestMapping("/searchThesis")
	public String searchThesis(@RequestParam String searchTerm, Model model ){
		model.addAttribute("thesiss",thesisDAO.searchByTerm(searchTerm));
		model.addAttribute("searchTerm", searchTerm);
		/*Thesis thesis;
		
		Set<Thesis> th = new HashSet<Thesis>();
		for(Thesis thesisList:th){
			for(Thesiscommittee thesiscommittee:thesisList.getThesiscommittees()){
				System.out.println("ThesisController.searchThesis()==========="+thesiscommittee.getFacultyByChair().getFacultyId());
			}
		}*/
		
		return "thesis/listThesiss";
	}
	
	@RequestMapping("/searchThesisByStudentId")
	public String searchThesisByStudentId(@RequestParam String id, Model model ){
		Set<Thesis> thesisSet = thesisDAO.searchThesisByStudentId(id);
		model.addAttribute("thesiss",thesisSet);
		return "thesis/listThesiss";
	}
	
	@RequestMapping("/searchThesisByCategoryId")
	public String searchThesisByCategoryId(@RequestParam int id, Model model){
		Set<Thesis> thesisSet = thesisDAO.searchThesisByCategoryId(id);
		model.addAttribute("thesiss",thesisSet);
		return "thesis/listThesiss";
	}
	
	@RequestMapping("/searchThesisByChair")
	public String searchThesisByChairId(@RequestParam String id, Model model){
		Set<Thesis> thesisSet = thesisDAO.searchThesisByChairId(id);
		for(Thesis thesis:thesisSet){
			System.out.println("ThesisController.searchThesisByChairId()==========="+thesis.getTitle());
		}
		model.addAttribute("thesiss",thesisSet);
		return "thesis/listThesiss";
	}
	
	
}